package javaz.collections.sort.collator;

import java.text.Collator;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

/**
 * @author natu
 * @date 2014-02-10
 * @url-1 http://blog.mwsoft.jp/article/36482331.html
 * @url-2 http://www.javapractices.com/topic/TopicAction.do?Id=207
 * @url-3 http://blog.ropardo.ro/2010/03/23/core-java-how-to-sort-strings-in-a-language-specific-order/
 */
public class CollatorSort {
	
	public static void main(String[] args) {
		String[] strArray2 = { "ぁ", "あ", "か", "が", "カ", "さ", "ざ" };
		List<String> list = Arrays.asList( strArray2 );

		// 通常のCollections.sortを使用した場合
		Collections.sort( list );
		System.out.println( list );
		  //=> [ぁ, あ, か, が, さ, ざ, カ]

		// Collatorを使用した場合
		// 小文字の「ぁ」は「あ」より後に来る
		// 平仮名の「か」、片仮名の「カ」、平仮名の「が」、平仮名の「さ」の順になる
		Collator collator = Collator.getInstance( Locale.JAPANESE );
		Collections.sort( list, collator );
		System.out.println( list );
		  //=> [あ, ぁ, か, カ, が, さ, ざ]

		collator.setStrength(Collator.PRIMARY);
		Collections.sort( list, collator );
		System.out.println( list );
	}

	public void sortAtistsByNameKana(List<Artist> artists) {
        final Collator collator = Collator.getInstance(Locale.JAPANESE);
        collator.setStrength(Collator.SECONDARY);
        Collections.sort(artists, new Comparator<Artist>() {
			@Override
			public int compare(Artist arg0, Artist arg1) {
				return collator.compare(arg0.getNameKana(), arg1.getNameKana());
			}
        });
    }
	
	public class Artist {
		public String getNameKana() {
			return null;
		}
	}

}
